package com.ruoyi.develop.lvDaohuo.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.finance.financeAp.service.IFinanceApService;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.goodsSalebuy.domain.GoodsSalebuy;
import com.ruoyi.develop.goodsSalebuy.service.IGoodsSalebuyService;
import com.ruoyi.develop.goodsSalebuyall.domain.GoodsSalebuyall;
import com.ruoyi.develop.goodsSalebuyall.service.IGoodsSalebuyallService;
import com.ruoyi.develop.goodsSaledo.domain.GoodsSaledo;
import com.ruoyi.develop.goodsSaledo.mapper.GoodsSaledoMapper;
import com.ruoyi.develop.lvBuy.domain.LvBuy;
import com.ruoyi.develop.lvBuy.service.ILvBuyService;
import com.ruoyi.develop.lvDaohuoDetail.domain.LvDaohuoDetail;
import com.ruoyi.develop.lvDaohuoDetail.service.ILvDaohuoDetailService;
import com.ruoyi.develop.lvRukumingxi.utils.RukumingxiUtils;
import com.ruoyi.develop.process.processArrival.service.IProcessArrivalService;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import com.ruoyi.develop.projectBaseInfo.service.impl.ProjectBaseInfoServiceImpl;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.lvDaohuo.domain.LvDaohuo;
import com.ruoyi.develop.lvDaohuo.service.ILvDaohuoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 到货单Controller
 *
 * @author qh
 * @date 2024-09-05
 */
@RestController
@RequestMapping("/lvDaohuo/lvDaohuo")
public class LvDaohuoController extends BaseController {
    @Autowired
    private ILvDaohuoService lvDaohuoService;
    @Autowired
    private ILvBuyService lvBuyService;
    @Autowired
    private IGoodsSalebuyallService goodsSalebuyallService;
    @Autowired
    private IGoodsSalebuyService goodsSalebuyService;
    @Autowired
    private IGoodsSaleService goodsSaleService;
    @Autowired
    private ILvDaohuoDetailService lvDaohuoDetailService;
    @Autowired
    private RukumingxiUtils RukumingxiUtils;
    @Autowired
    private GoodsSaledoMapper goodsSaledoMapper;
    @Autowired
    private ProjectBaseInfoServiceImpl projectBaseInfoService;
    @Autowired
    private IFinanceApService financeApService;
    @Autowired
    private IProcessArrivalService processArrivalService;
    @Autowired
    private ISysUserService sysUserService;


    /**
     * 查询到货单列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuo:query')")
    @GetMapping("/list")
    public TableDataInfo list(LvDaohuo entity) {
        startPage();
        List<LvDaohuo> list = lvDaohuoService.selectList(entity);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('develop:LvDaohuo:query')")
    @GetMapping("/listYunfei")
    public TableDataInfo listYunfei(LvDaohuo entity) {
        startPage();
        List<LvDaohuo> list = lvDaohuoService.listYunfei(entity);
        return getDataTable(list);
    }

    /**
     * 导出到货单列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuo:export')")
    @Log(title = "到货单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LvDaohuo entity) {
        List<LvDaohuo> list = lvDaohuoService.selectList(entity);
        ExcelUtil<LvDaohuo> util = new ExcelUtil<LvDaohuo>(LvDaohuo.class);
        util.exportExcel(response, list, "到货单数据");
    }

    /**
     * 获取到货单详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(lvDaohuoService.selectById(id));
    }

    /**
     * 新增到货
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuo:add')")
    @Log(title = "到货单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @Transactional
    public AjaxResult add(@RequestBody LvDaohuo entity) {
        LvBuy lvBuy = lvBuyService.selectById(entity.getBuyId());
        // 新增 到货 记录 和 到货明细
        // 直发货的 质检状态不用质检
        if (BusinessConstants.FAHUO_TYPE_1.equals(entity.getFahuoType())) {//直发货
            entity.setStatus(BusinessConstants.ARRIVE_STATUS_TEST_PASS);
        } else {
            entity.setStatus(BusinessConstants.ARRIVE_STATUS_ED);
        }
        if (lvBuy != null) {
            entity.setSupplierId(lvBuy.getSupplierId());
            entity.setSupplierName(lvBuy.getSupplierName());
            entity.setAccountNo(lvBuy.getAccountNo());
            entity.setBankName(lvBuy.getBankName());
            entity.setBankNo(lvBuy.getBankNo());
            entity.setYwType(lvBuy.getYwType());
            entity.setBuyNo(lvBuy.getNos());
        }
        int i = lvDaohuoService.insert(entity);
        processArrivalService.insertProcess(entity.getId(), "", "部分到货");
        List<GoodsSalebuyall> goodsSalebuyallList = entity.getGoodsSalebuyallList();
        BigDecimal totalPrice = BigDecimal.ZERO;

        int allArrivedCount = 0;
        for (GoodsSalebuyall goodsSalebuyall : goodsSalebuyallList) {
            // 等于0的是没有到货 不需要处理
            if (goodsSalebuyall.getDaohuoCountTemp().compareTo(BigDecimal.ZERO) != 0 || goodsSalebuyall.getDaohuoDun().compareTo(BigDecimal.ZERO) != 0) {
                LvDaohuoDetail lvDaohuoDetail = new LvDaohuoDetail();
                lvDaohuoDetail.setSaleId(goodsSalebuyall.getId());
                lvDaohuoDetail.setId(IdUtils.fastSimpleUUID());
                lvDaohuoDetail.setDaohuoId(entity.getId());
                lvDaohuoDetail.setGoodId(goodsSalebuyall.getGoodsId());
                lvDaohuoDetail.setCreateBy(SecurityUtils.getUsername());
                lvDaohuoDetail.setCreateTime(DateUtils.getNowDate());
                lvDaohuoDetail.setDaohuoCount(goodsSalebuyall.getDaohuoCountTemp());
                lvDaohuoDetail.setDaohuoDun(goodsSalebuyall.getDaohuoDunTemp());
                lvDaohuoDetail.setUnitPrice(goodsSalebuyall.getGoodsBuyPrice());
                lvDaohuoDetail.setTotalPrice(goodsSalebuyall.getDaohuoMoney());
                lvDaohuoDetail.setCanCount(goodsSalebuyall.getDaohuoCountTemp());
                lvDaohuoDetail.setProjectId(goodsSalebuyall.getProjectId());
                lvDaohuoDetail.setProjectNo(goodsSalebuyall.getProjectNo());
                lvDaohuoDetail.setGoodsSaleId(goodsSalebuyall.getGoodsSaleId());
                lvDaohuoDetail.setSupplierId(lvBuy.getSupplierId());
                lvDaohuoDetail.setSupplierName(lvBuy.getSupplierName());
                lvDaohuoDetail.setFahuoType(entity.getFahuoType());
                if (BusinessConstants.FAHUO_TYPE_1.equals(entity.getFahuoType())) {//直发货
                    lvDaohuoDetail.setInspectState(BusinessConstants.INSPECT_STATE_YES);//已质检
                } else {
                    lvDaohuoDetail.setInspectState(BusinessConstants.INSPECT_STATE_UN);//待质检
                }
                lvDaohuoDetailService.insert(lvDaohuoDetail);
                // 更新商品的 到货情况
                BigDecimal shengyuDaohuoCount = goodsSalebuyall.getShengyuDaohuoCount().subtract(goodsSalebuyall.getDaohuoCountTemp());
                BigDecimal shengyuDaohuoDun = goodsSalebuyall.getShengyuDaohuoDun().subtract(goodsSalebuyall.getDaohuoDunTemp());
                // 如果剩余到货数量为小于等于0，则更新采购表的状态为全部到货

                if (BusinessConstants.GOODS_TYPE_HARDWARE.equals(goodsSalebuyall.getGoodsType())) {
                    if (shengyuDaohuoCount.compareTo(BigDecimal.ZERO) <= 0) {
                        goodsSalebuyall.setShengyuDaohuoCount(BigDecimal.ZERO);
                        goodsSalebuyall.setDaohuoStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_ALL);
                        allArrivedCount++;
                    }else{
                        goodsSalebuyall.setDaohuoStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_PART);
                    }
                }
                if (BusinessConstants.GOODS_TYPE_MATERIAL.equals(goodsSalebuyall.getGoodsType())) {
                    if (shengyuDaohuoDun.compareTo(BigDecimal.ZERO) <= 0) {
                        goodsSalebuyall.setShengyuDaohuoDun(BigDecimal.ZERO);
                        goodsSalebuyall.setDaohuoStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_ALL);
                        allArrivedCount++;
                    }else{
                        goodsSalebuyall.setDaohuoStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_PART);
                    }
                }

                goodsSalebuyall.setDaohuoId(lvDaohuoDetail.getId());
                goodsSalebuyall.setDakuanno(lvDaohuoDetail.getNos());
                goodsSalebuyall.setDaohuoCount(goodsSalebuyall.getDaohuoCount().add(goodsSalebuyall.getDaohuoCountTemp()));
                goodsSalebuyall.setDaohuoDun(goodsSalebuyall.getDaohuoDun().add(goodsSalebuyall.getDaohuoDunTemp()));
                goodsSalebuyall.setShengyuDaohuoDun(shengyuDaohuoDun);
                goodsSalebuyall.setShengyuDaohuoCount(shengyuDaohuoCount);
                goodsSalebuyall.setTuihuoType(entity.getFahuoType());
                totalPrice = totalPrice.add(goodsSalebuyall.getDaohuoMoney());
                goodsSalebuyallService.update(goodsSalebuyall);

                List<GoodsSalebuy> goodsSalebuyList = goodsSalebuyService.selectByCgallId(goodsSalebuyall.getId());
                for (GoodsSalebuy goodsSalebuy : goodsSalebuyList) {
                    GoodsSaledo goodsSaledo = goodsSaledoMapper.selectById(goodsSalebuy.getQinggouId());
                    goodsSaledo.setStatus(BusinessConstants.SALE_TYPE_10);
                    goodsSaledo.setDaohuoId(lvDaohuoDetail.getId());
                    goodsSaledo.setDaohuono(lvDaohuoDetail.getNos());
                    goodsSaledoMapper.update(goodsSaledo);
                    goodsSalebuy.setDaohuoId(lvDaohuoDetail.getId());
                    goodsSalebuy.setDakuanno(lvDaohuoDetail.getNos());
                    goodsSalebuyService.update(goodsSalebuy);
                    if (org.apache.commons.lang.StringUtils.isNotBlank(goodsSalebuy.getGoodsSaleId())) {
                        GoodsSale goodsSale = goodsSaleService.selectById(goodsSalebuy.getGoodsSaleId());
                        if (goodsSale != null) {
                            if (BusinessConstants.FAHUO_TYPE_1.equals(entity.getFahuoType())) {//直发货
                                goodsSaleService.updateStatusById(BusinessConstants.SALE_TYPE_8, goodsSale.getId());//已到货的不再更改状态
                            } else {
                                goodsSale.setStatus(BusinessConstants.SALE_TYPE_10);//采购到货
                            }
                            goodsSale.setCaigouType(entity.getFahuoType());
                            goodsSaleService.update(goodsSale);
                        }
                    }
                }
            }
        }
        if (allArrivedCount == goodsSalebuyallList.size()) {
            lvBuy.setArrivalStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_ALL);
        } else {
            lvBuy.setArrivalStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_PART);
        }
        lvBuy.setType(entity.getFahuoType());
        lvBuy.setActualArrivalTotalMoney(lvBuy.getActualArrivalTotalMoney().add(totalPrice));
        lvBuyService.update(lvBuy);
        entity.setDaohuoMoney(totalPrice);
        lvDaohuoService.update(entity);
        if (i > 0) {
            return AjaxResult.success("部分到货成功！");
        } else {
            return AjaxResult.error("部分到货失败！");
        }
    }


    /**
     * 采购单一次全部到货
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuo:add')")
    @Log(title = "采购单一次全部到货", businessType = BusinessType.INSERT)
    @Transactional
    @PostMapping("/daohuoAddAll")
    public AjaxResult daohuoAddAll(@RequestBody LvDaohuo entity) {
        LvBuy lvBuy = lvBuyService.selectById(entity.getBuyId());
        // 新增 到货 记录 和 到货明细
        // 直发货的 质检状态不用质检
        if (BusinessConstants.FAHUO_TYPE_1.equals(entity.getFahuoType())) {//直发货
            entity.setStatus(BusinessConstants.ARRIVE_STATUS_TEST_PASS);
        } else {
            entity.setStatus(BusinessConstants.ARRIVE_STATUS_ED);
        }
        if (lvBuy != null) {
            entity.setSupplierId(lvBuy.getSupplierId());
            entity.setSupplierName(lvBuy.getSupplierName());
            entity.setAccountNo(lvBuy.getAccountNo());
            entity.setBankName(lvBuy.getBankName());
            entity.setBankNo(lvBuy.getBankNo());
            entity.setYwType(lvBuy.getYwType());
            entity.setBuyNo(lvBuy.getNos());
        }
        int i = lvDaohuoService.insert(entity);
        processArrivalService.insertProcess(entity.getId(), "", "全部到货");
        List<GoodsSalebuyall> goodsSalebuyallList = entity.getGoodsSalebuyallList();
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (GoodsSalebuyall goodsSalebuyall : goodsSalebuyallList) {
            // 等于0的是没有到货 不需要处理
            if (goodsSalebuyall.getDaohuoCountTemp() == null || goodsSalebuyall.getDaohuoCountTemp().compareTo(BigDecimal.ZERO) != 0) {
                LvDaohuoDetail lvDaohuoDetail = new LvDaohuoDetail();
                lvDaohuoDetail.setSaleId(goodsSalebuyall.getId());
                lvDaohuoDetail.setId(IdUtils.fastSimpleUUID());
                lvDaohuoDetail.setDaohuoId(entity.getId());
                lvDaohuoDetail.setGoodId(goodsSalebuyall.getGoodsId());
                lvDaohuoDetail.setCreateBy(SecurityUtils.getUsername());
                lvDaohuoDetail.setCreateTime(DateUtils.getNowDate());
                lvDaohuoDetail.setDaohuoCount(goodsSalebuyall.getDaohuoCountTemp());
                lvDaohuoDetail.setDaohuoDun(goodsSalebuyall.getDaohuoDunTemp());
                lvDaohuoDetail.setUnitPrice(goodsSalebuyall.getGoodsBuyPrice());
                lvDaohuoDetail.setTotalPrice(goodsSalebuyall.getDaohuoMoney());
                lvDaohuoDetail.setCanCount(goodsSalebuyall.getDaohuoCountTemp());
                if(goodsSalebuyall.getTaxRate()!=null){
                    lvDaohuoDetail.setTaxRate(goodsSalebuyall.getTaxRate());
                    lvDaohuoDetail.setTotalPriceNoTax(goodsSalebuyall.getDaohuoMoney().divide(BigDecimal.ONE.add(goodsSalebuyall.getTaxRate()),4, RoundingMode.HALF_UP));
                    lvDaohuoDetail.setUnitPriceNoTax(goodsSalebuyall.getGoodsBuyPrice().divide(BigDecimal.ONE.add(goodsSalebuyall.getTaxRate()),4, RoundingMode.HALF_UP));
                    lvDaohuoDetail.setSingleWeight(goodsSalebuyall.getDaohuoDunTemp().divide(goodsSalebuyall.getDaohuoCountTemp(),4, RoundingMode.HALF_UP));
                }
                lvDaohuoDetail.setProjectId(goodsSalebuyall.getProjectId());
                lvDaohuoDetail.setProjectNo(goodsSalebuyall.getProjectNo());
                lvDaohuoDetail.setGoodsSaleId(goodsSalebuyall.getGoodsSaleId());
                lvDaohuoDetail.setSupplierId(lvBuy.getSupplierId());
                lvDaohuoDetail.setSupplierName(lvBuy.getSupplierName());
                lvDaohuoDetail.setFahuoType(entity.getFahuoType());
                if (BusinessConstants.FAHUO_TYPE_1.equals(entity.getFahuoType())) {//直发货
                    lvDaohuoDetail.setInspectState(BusinessConstants.INSPECT_STATE_YES);//已质检
                } else {
                    lvDaohuoDetail.setInspectState(BusinessConstants.INSPECT_STATE_UN);//待质检
                }
                lvDaohuoDetailService.insert(lvDaohuoDetail);
                // 更新商品的 到货情况
                if (goodsSalebuyall.getShengyuDaohuoCount() != null && goodsSalebuyall.getDaohuoCountTemp() != null) {
                    BigDecimal shengyuDaohuoCount = goodsSalebuyall.getShengyuDaohuoCount().subtract(goodsSalebuyall.getDaohuoCountTemp());
                    // 如果剩余到货数量为小于等于0，则更新采购表的状态为全部到货
                    if (shengyuDaohuoCount.compareTo(BigDecimal.ZERO) <= 0) {
                        goodsSalebuyall.setShengyuDaohuoCount(BigDecimal.ZERO);
                        goodsSalebuyall.setDaohuoStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_ALL);
                    }
                    goodsSalebuyall.setShengyuDaohuoCount(shengyuDaohuoCount);
                }
                if (goodsSalebuyall.getShengyuDaohuoDun() != null && goodsSalebuyall.getDaohuoDunTemp() != null) {
                    BigDecimal shengyuDaohuoDun = goodsSalebuyall.getShengyuDaohuoDun().subtract(goodsSalebuyall.getDaohuoDunTemp());
                    goodsSalebuyall.setShengyuDaohuoDun(shengyuDaohuoDun);
                }
                goodsSalebuyall.setDaohuoId(lvDaohuoDetail.getId());
                goodsSalebuyall.setDaohuono(lvDaohuoDetail.getNos());
                if (goodsSalebuyall.getDaohuoCount() != null && goodsSalebuyall.getDaohuoCountTemp() != null) {
                    goodsSalebuyall.setDaohuoCount(goodsSalebuyall.getDaohuoCount().add(goodsSalebuyall.getDaohuoCountTemp()));
                }
                if (goodsSalebuyall.getDaohuoDun() != null && goodsSalebuyall.getDaohuoDunTemp() != null) {
                    goodsSalebuyall.setDaohuoDun(goodsSalebuyall.getDaohuoDun().add(goodsSalebuyall.getDaohuoDunTemp()));
                }
                goodsSalebuyall.setTuihuoType(entity.getFahuoType());
                totalPrice = totalPrice.add(goodsSalebuyall.getGoodsBuyTotal());
                goodsSalebuyallService.update(goodsSalebuyall);

                List<GoodsSalebuy> goodsSalebuyList = goodsSalebuyService.selectByCgallId(goodsSalebuyall.getId());
                for (GoodsSalebuy goodsSalebuy : goodsSalebuyList) {
                    GoodsSaledo goodsSaledo = goodsSaledoMapper.selectById(goodsSalebuy.getQinggouId());
                    goodsSaledo.setStatus(BusinessConstants.SALE_TYPE_10);
                    goodsSaledo.setDaohuoId(lvDaohuoDetail.getId());
                    goodsSaledo.setDaohuono(lvDaohuoDetail.getNos());
                    goodsSaledoMapper.update(goodsSaledo);
                    goodsSalebuy.setDaohuoId(lvDaohuoDetail.getId());
                    goodsSalebuy.setDakuanno(lvDaohuoDetail.getNos());
                    goodsSalebuyService.update(goodsSalebuy);
                    if (org.apache.commons.lang.StringUtils.isNotBlank(goodsSalebuy.getGoodsSaleId())) {
                        GoodsSale goodsSale = goodsSaleService.selectById(goodsSalebuy.getGoodsSaleId());
                        if (goodsSale != null) {
                            if (BusinessConstants.FAHUO_TYPE_1.equals(entity.getFahuoType())) {//直发货
                                //goodsSale.setStatus(BusinessConstants.SALE_TYPE_8);//待入库
                                goodsSaleService.updateStatusById(BusinessConstants.SALE_TYPE_8, goodsSale.getId());//已到货的不再更改状态
                            } else {
                                goodsSale.setStatus(BusinessConstants.SALE_TYPE_10);//采购到货
                            }
                            goodsSale.setCaigouType(entity.getFahuoType());
                            goodsSaleService.update(goodsSale);
                        }
                    }
                    //计算
                    GoodsSale goodsSale = goodsSaleService.selectById(goodsSalebuy.getGoodsSaleId());
                    if (goodsSale != null) {
                        ProjectBaseInfo projectBaseInfo = projectBaseInfoService.selectById(goodsSale.getProjectId());
                        projectBaseInfo.setBuyMoney(projectBaseInfo.getBuyMoney().add(goodsSalebuy.getGoodsBuyPriceAll().multiply(goodsSale.getOrderCount())));
                        projectBaseInfo.setProfitMoney(projectBaseInfo.getBackMoney().subtract(projectBaseInfo.getBuyMoney()).subtract(projectBaseInfo.getFeeMoney()));
                        projectBaseInfoService.updateBackMoney(projectBaseInfo);
                    }
                }
            }
        }

        lvBuy.setArrivalStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_ALL);
        lvBuy.setType(entity.getFahuoType());
        lvBuy.setActualArrivalTotalMoney(totalPrice);
        lvBuyService.update(lvBuy);
        if (i > 0) {
            return AjaxResult.success("全部到货成功！");
        } else {
            return AjaxResult.error("全部到货失败！");
        }
    }


    /**
     * 采购单退货
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuo:add')")
    @Log(title = "采购单退货", businessType = BusinessType.INSERT)
    @Transactional
    @PostMapping("/returnByBuy")
    public AjaxResult returnByBuy(@RequestBody LvDaohuo entity) {
        LvBuy lvBuy = lvBuyService.selectById(entity.getBuyId());
        // 退货的不用质检
        entity.setStatus(BusinessConstants.ARRIVE_STATUS_TEST_PASS);

        if (lvBuy != null) {
            entity.setSupplierId(lvBuy.getSupplierId());
            entity.setSupplierName(lvBuy.getSupplierName());
            entity.setAccountNo(lvBuy.getAccountNo());
            entity.setBankName(lvBuy.getBankName());
            entity.setBankNo(lvBuy.getBankNo());
            entity.setYwType(lvBuy.getYwType());
            entity.setBuyNo(lvBuy.getNos());
        }
        int i = lvDaohuoService.insert(entity);
        processArrivalService.insertProcess(entity.getId(), "", "采购退货");
        List<GoodsSalebuyall> goodsSalebuyallList = entity.getGoodsSalebuyallList();
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (GoodsSalebuyall goodsSalebuyall : goodsSalebuyallList) {
            // 等于0的是没有到货 不需要处理
            LvDaohuoDetail lvDaohuoDetail = new LvDaohuoDetail();
            lvDaohuoDetail.setSaleId(goodsSalebuyall.getId());
            lvDaohuoDetail.setId(IdUtils.fastSimpleUUID());
            lvDaohuoDetail.setDaohuoId(entity.getId());
            lvDaohuoDetail.setGoodId(goodsSalebuyall.getGoodsId());
            lvDaohuoDetail.setCreateBy(SecurityUtils.getUsername());
            lvDaohuoDetail.setCreateTime(DateUtils.getNowDate());
            lvDaohuoDetail.setDaohuoCount(goodsSalebuyall.getShengyu());
            lvDaohuoDetail.setDaohuoDun(goodsSalebuyall.getDun());
            lvDaohuoDetail.setUnitPrice(goodsSalebuyall.getGoodsBuyPrice());
            lvDaohuoDetail.setTotalPrice(goodsSalebuyall.getGoodsBuyTotal());
            lvDaohuoDetail.setCanCount(BigDecimal.ZERO);
            lvDaohuoDetail.setProjectId(goodsSalebuyall.getProjectId());
            lvDaohuoDetail.setProjectNo(goodsSalebuyall.getProjectNo());
            lvDaohuoDetail.setGoodsSaleId(goodsSalebuyall.getGoodsSaleId());
            lvDaohuoDetail.setSupplierId(lvBuy.getSupplierId());
            lvDaohuoDetail.setSupplierName(lvBuy.getSupplierName());
            lvDaohuoDetail.setFahuoType(entity.getFahuoType());
            lvDaohuoDetail.setInspectState(BusinessConstants.INSPECT_STATE_YES);//已质检
            lvDaohuoDetailService.insert(lvDaohuoDetail);
            // 更新商品的 到货情况
            goodsSalebuyall.setShengyuDaohuoCount(BigDecimal.ZERO);
            goodsSalebuyall.setShengyuDaohuoDun(BigDecimal.ZERO);
            goodsSalebuyall.setDaohuoCount(goodsSalebuyall.getShengyu());
            goodsSalebuyall.setDaohuoDun(goodsSalebuyall.getDun());
            goodsSalebuyall.setDaohuoStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_ALL);
            goodsSalebuyall.setDaohuoId(lvDaohuoDetail.getId());
            goodsSalebuyall.setDaohuono(lvDaohuoDetail.getNos());
            totalPrice = totalPrice.add(goodsSalebuyall.getGoodsBuyTotal());
            goodsSalebuyallService.update(goodsSalebuyall);
            List<GoodsSalebuy> goodsSalebuyList = goodsSalebuyService.selectByCgallId(goodsSalebuyall.getId());
            for (GoodsSalebuy goodsSalebuy : goodsSalebuyList) {
                goodsSalebuy.setDaohuoId(lvDaohuoDetail.getId());
                goodsSalebuy.setDaohuono(lvDaohuoDetail.getNos());
                goodsSalebuyService.update(goodsSalebuy);
            }
        }

        lvBuy.setArrivalStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_ALL);
        lvBuy.setReturnStatus(BusinessConstants.RETURN_STATUS_APPLY);
        lvBuy.setType(entity.getFahuoType());
        lvBuy.setActualArrivalTotalMoney(totalPrice);
        lvBuyService.update(lvBuy);
        if (i > 0) {
            return AjaxResult.success("退货成功！");
        } else {
            return AjaxResult.error("退货失败！");
        }
    }


    @Log(title = "到货单", businessType = BusinessType.INSERT)
    @PostMapping("/zhifahuoAdd")
    public AjaxResult zhifahuoAdd(@RequestBody LvDaohuo entity) {
        int i = lvDaohuoService.insertZhifahuo(entity);
        if (i > 0) {
            return AjaxResult.success("直发货成功！");
        } else {
            return AjaxResult.error("直发货失败！");
        }
    }

    /**
     * 修改到货单
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuo:edit')")
    @Log(title = "到货单", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LvDaohuo entity) {
        return toAjax(lvDaohuoService.update(entity));
    }

    /**
     * 删除到货单
     */
    @PreAuthorize("@ss.hasPermi('develop:LvDaohuo:remove')")
    @Log(title = "到货单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lvDaohuoService.deleteByIds(ids));
    }

    @PutMapping("/editYf")
    public AjaxResult editYf(@RequestBody LvDaohuo entity) {
        return toAjax(lvDaohuoService.updateYf(entity));
    }


    /**
     * 采购审核通过
     */
    @GetMapping(value = "/buyPass/{id}")
    @Transactional
    public AjaxResult buyPass(@PathVariable("id") String id) {
        LvDaohuo lvDaohuo = lvDaohuoService.selectById(id);

        lvDaohuo.setStatus(BusinessConstants.ARRIVE_STATUS_TEST_PASS);
        lvDaohuoService.update(lvDaohuo);

        List<GoodsSaledo> goodsSaledoList = goodsSaledoMapper.selectByDaohuoId(lvDaohuo.getId());
        for (GoodsSaledo goodsSaledo : goodsSaledoList) {
            goodsSaledo.setStatus(BusinessConstants.SALE_TYPE_10);
            goodsSaledoMapper.update(goodsSaledo);
            if (goodsSaledo != null && goodsSaledo.getGoodsSaleId() != null) {
                goodsSaleService.updateStatusById(goodsSaledo.getGoodsSaleId(), BusinessConstants.SALE_TYPE_10);
            }
        }

        return AjaxResult.success("审核通过");
    }


    /**
     * 采购审核退回
     */
    @GetMapping(value = "/buyRefuse/{id}")
    @Transactional
    public AjaxResult buyRefuse(@PathVariable("id") String id) {

        LvDaohuo lvDaohuo = lvDaohuoService.selectById(id);
        processArrivalService.insertProcess(lvDaohuo.getId(), "", "采购退回");
        LvBuy lvBuy = lvBuyService.selectById(lvDaohuo.getBuyId());

        // 对应采购单如果已经付款 则不能退回 TODO
        if (BusinessConstants.BUY_PAY_STATUS_NOT.equals(lvBuy.getPayApplyState())) {

        }

        GoodsSalebuyall goodsSalebuyall = new GoodsSalebuyall();
        goodsSalebuyall.setMainId(lvDaohuo.getBuyId());
        List<GoodsSalebuyall> goodsSalebuyallList = goodsSalebuyallService.selectList(goodsSalebuyall);

        LvDaohuoDetail lvDaohuoDetail = new LvDaohuoDetail();
        lvDaohuoDetail.setDaohuoId(id);
        List<LvDaohuoDetail> list = lvDaohuoDetailService.selectList(lvDaohuoDetail);
        for (LvDaohuoDetail daohuoDetail : list) {
            for (GoodsSalebuyall buyDetail : goodsSalebuyallList) {
                if (buyDetail.getGoodsId().equals(daohuoDetail.getGoodId())) {
                    buyDetail.setDaohuoCount(buyDetail.getDaohuoCount().subtract(daohuoDetail.getDaohuoCount()));
                    buyDetail.setDaohuoDun(buyDetail.getDaohuoDun().subtract(daohuoDetail.getDaohuoDun()));
                    buyDetail.setShengyuDaohuoCount(buyDetail.getShengyuDaohuoCount().add(daohuoDetail.getDaohuoCount()));
                    buyDetail.setShengyuDaohuoDun(buyDetail.getShengyuDaohuoDun().add(daohuoDetail.getDaohuoDun()));
                    if (buyDetail.getDaohuoCount().compareTo(BigDecimal.ZERO) == 0) {
                        buyDetail.setDaohuoStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_NOT);
                    } else {
                        buyDetail.setDaohuoStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_PART);
                    }
                    goodsSalebuyallService.update(buyDetail);
                }
            }
            daohuoDetail.setInspectState(BusinessConstants.ARRIVE_STATUS_BUY_REFUSE);
            lvDaohuoDetailService.update(daohuoDetail);
            lvDaohuoDetailService.deleteById(daohuoDetail.getId());
        }

        lvBuy.setArrivalStatus(BusinessConstants.GOODS_ARRIVAL_STATUS_PART);
        lvBuy.setActualArrivalTotalMoney(lvBuy.getActualArrivalTotalMoney().subtract(lvDaohuo.getDaohuoMoney()));
        lvBuyService.update(lvBuy);

        lvDaohuo.setStatus(BusinessConstants.ARRIVE_STATUS_BUY_REFUSE);
        lvDaohuoService.update(lvDaohuo);
        lvDaohuoService.deleteById(id);

        List<GoodsSaledo> goodsSaledoList = goodsSaledoMapper.selectByDaohuoId(lvDaohuo.getId());
        for (GoodsSaledo goodsSaledo : goodsSaledoList) {
            goodsSaledo.setStatus(BusinessConstants.SALE_TYPE_12);
            goodsSaledoMapper.update(goodsSaledo);
            if (goodsSaledo != null && goodsSaledo.getGoodsSaleId() != null) {
                goodsSaleService.updateStatusById(goodsSaledo.getGoodsSaleId(), BusinessConstants.SALE_TYPE_12);
            }
        }

        return AjaxResult.success("审核通过");
    }

    /**
     * 质检审核通过
     */
    @GetMapping(value = "/testPass/{id}")
    @Transactional
    public AjaxResult testPass(@PathVariable("id") String id) {
        LvDaohuo lvDaohuo = lvDaohuoService.selectById(id);

        if (BusinessConstants.ARRIVE_STATUS_TEST_PASS.equals(lvDaohuo.getStatus()) || BusinessConstants.ARRIVE_STATUS_TEST_REJECT.equals(lvDaohuo.getStatus())) {
            return AjaxResult.error("该入库单已经审核！");
        }

        if (!(BusinessConstants.ARRIVE_STATUS_ED.equals(lvDaohuo.getStatus()) || BusinessConstants.ARRIVE_STATUS_STORE_REJECT.equals(lvDaohuo.getStatus()))) {
            return AjaxResult.error("该入库单状态异常，请刷新页面！");
        }

        processArrivalService.insertProcess(lvDaohuo.getId(), "", "质检通过");
        lvDaohuo.setStatus(BusinessConstants.ARRIVE_STATUS_TEST_PASS);
        lvDaohuo.setZhijianId(SecurityUtils.getUserId());
        lvDaohuo.setZhijianName(SecurityUtils.getUsername());
        lvDaohuoService.update(lvDaohuo);

        LvDaohuoDetail lvDaohuoDetail = new LvDaohuoDetail();
        lvDaohuoDetail.setDaohuoId(id);
        List<LvDaohuoDetail> list = lvDaohuoDetailService.selectList(lvDaohuoDetail);
        for (LvDaohuoDetail daohuoDetail : list) {
            daohuoDetail.setZhijianId(SecurityUtils.getUserId());
            daohuoDetail.setZhijianName(SecurityUtils.getUsername());
            daohuoDetail.setInspectState(BusinessConstants.ARRIVE_STATUS_TEST_PASS);
            lvDaohuoDetailService.update(daohuoDetail);
        }
        List<GoodsSaledo> goodsSaledoList = goodsSaledoMapper.selectByDaohuoId(lvDaohuo.getId());
        for (GoodsSaledo goodsSaledo : goodsSaledoList) {
            goodsSaledo.setStatus(BusinessConstants.SALE_TYPE_8);
            goodsSaledoMapper.update(goodsSaledo);
            if (goodsSaledo != null && goodsSaledo.getGoodsSaleId() != null) {
                goodsSaleService.updateStatusById(goodsSaledo.getGoodsSaleId(), BusinessConstants.SALE_TYPE_8);
            }
        }
        return AjaxResult.success("审核通过");
    }

    /**
     * 质检审核弃审
     */
    @GetMapping(value = "/testRefuse/{id}")
    @Transactional
    public AjaxResult testRefuse(@PathVariable("id") String id) {
        LvDaohuo lvDaohuo = lvDaohuoService.selectById(id);
        processArrivalService.insertProcess(lvDaohuo.getId(), "", "质检退回");
        // 对应采购单如果已经付款 则不能退回
        LvBuy lvBuy = lvBuyService.selectById(lvDaohuo.getBuyId());
        String payApplyState = lvBuy.getPayApplyState();
        if (BusinessConstants.BUY_PAY_STATUS_ALL_APPLY.equals(payApplyState)
                || BusinessConstants.BUY_PAY_STATUS_DEPOSIT_APPLY.equals(payApplyState)
                || BusinessConstants.BUY_PAY_STATUS_DEPOSIT_PAID.equals(payApplyState)
                || BusinessConstants.BUY_PAY_STATUS_PART.equals(payApplyState)
                || BusinessConstants.BUY_PAY_STATUS_PAID.equals(payApplyState)
        ) {
            return AjaxResult.error("该采购单已经申请付款，不能退回！");
        }

        if (BusinessConstants.ARRIVE_STATUS_TEST_PASS.equals(lvDaohuo.getStatus()) || BusinessConstants.ARRIVE_STATUS_TEST_REJECT.equals(lvDaohuo.getStatus())) {
            return AjaxResult.error("该入库单已经审核！");
        }

        if (!(BusinessConstants.ARRIVE_STATUS_ED.equals(lvDaohuo.getStatus()) || BusinessConstants.ARRIVE_STATUS_STORE_REJECT.equals(lvDaohuo.getStatus()))) {
            return AjaxResult.error("该入库单状态异常，请刷新页面！");
        }

        lvDaohuo.setStatus(BusinessConstants.ARRIVE_STATUS_TEST_REJECT);
        lvDaohuo.setZhijianId(SecurityUtils.getUserId());
        lvDaohuo.setZhijianName(SecurityUtils.getUsername());
        lvDaohuoService.update(lvDaohuo);

        LvDaohuoDetail lvDaohuoDetail = new LvDaohuoDetail();
        lvDaohuoDetail.setDaohuoId(id);
        List<LvDaohuoDetail> daohuoDetailList = lvDaohuoDetailService.selectList(lvDaohuoDetail);
        for (LvDaohuoDetail daohuoDetail : daohuoDetailList) {
            lvDaohuoDetail.setZhijianId(SecurityUtils.getUserId());
            lvDaohuoDetail.setZhijianName(SecurityUtils.getUsername());
            daohuoDetail.setInspectState(BusinessConstants.ARRIVE_STATUS_TEST_REJECT);
            lvDaohuoDetailService.update(daohuoDetail);
        }

        List<GoodsSaledo> goodsSaledoList = goodsSaledoMapper.selectByDaohuoId(lvDaohuo.getId());
        for (GoodsSaledo goodsSaledo : goodsSaledoList) {
            goodsSaledo.setStatus(BusinessConstants.SALE_TYPE_12);
            goodsSaledoMapper.update(goodsSaledo);
            if (goodsSaledo != null && goodsSaledo.getGoodsSaleId() != null) {
                goodsSaleService.updateStatusById(goodsSaledo.getGoodsSaleId(), BusinessConstants.SALE_TYPE_12);
            }
        }

        return AjaxResult.success("审核通过");
    }

    /**
     * 库管审核通过
     */
    @GetMapping(value = "/storePass/{id}/{shourenName}")
    @Transactional
    public AjaxResult storePass(@PathVariable("id") String id, @PathVariable("shourenName") String shourenName) {
        LvDaohuo lvDaohuo = lvDaohuoService.selectById(id);

        if (BusinessConstants.ARRIVE_STATUS_STORE_PASS.equals(lvDaohuo.getStatus())  || BusinessConstants.ARRIVE_STATUS_STORE_REJECT.equals(lvDaohuo.getStatus())) {
            return AjaxResult.error("该入库单已经审核！");
        }
        if (!BusinessConstants.ARRIVE_STATUS_TEST_PASS.equals(lvDaohuo.getStatus())) {
            return AjaxResult.error("该入库单未通过质检！");
        }
        processArrivalService.insertProcess(lvDaohuo.getId(), "", "库管通过");
        // 入库审核通过 增加库存
        lvDaohuo.setStatus(BusinessConstants.ARRIVE_STATUS_STORE_PASS);
        if (StringUtils.isNotBlank(shourenName)) {
            SysUser sysUser = sysUserService.selectUserByUserName(shourenName);
            if (sysUser != null) {
                lvDaohuo.setShouhuoId(sysUser.getUserId());
                lvDaohuo.setShourenName(sysUser.getUserName());
            }
        }
        lvDaohuoService.update(lvDaohuo);

        List<GoodsSaledo> goodsSaledoList = goodsSaledoMapper.selectByDaohuoId(lvDaohuo.getId());
        for (GoodsSaledo goodsSaledo : goodsSaledoList) {
            goodsSaledo.setStatus(BusinessConstants.SALE_TYPE_9);
            goodsSaledoMapper.update(goodsSaledo);
            if (goodsSaledo != null && goodsSaledo.getGoodsSaleId() != null) {
                goodsSaleService.updateStatusById(goodsSaledo.getGoodsSaleId(), BusinessConstants.SALE_TYPE_9);
            }
        }
        LvDaohuoDetail lvDaohuoDetail = new LvDaohuoDetail();
        lvDaohuoDetail.setDaohuoId(id);
        List<LvDaohuoDetail> list = lvDaohuoDetailService.selectList(lvDaohuoDetail);
        for (LvDaohuoDetail detail : list) {
            detail.setShouhuoId(SecurityUtils.getUserId());
            detail.setShourenName(SecurityUtils.getUsername());
            detail.setInspectState(BusinessConstants.ARRIVE_STATUS_STORE_PASS);
            lvDaohuoDetailService.update(detail);
        }
        lvDaohuoDetailService.rukuByDaohuo(lvDaohuo, list);

        return AjaxResult.success("审核通过");
    }


    /**
     * 库管审核弃审
     */
    @GetMapping(value = "/storeRefuse/{id}")
    @Transactional
    public AjaxResult storeRefuse(@PathVariable("id") String id) {
        LvDaohuo lvDaohuo = lvDaohuoService.selectById(id);

        // 对应采购单如果已经付款 则不能退回
        LvBuy lvBuy = lvBuyService.selectById(lvDaohuo.getBuyId());
        String payApplyState = lvBuy.getPayApplyState();
        if (BusinessConstants.BUY_PAY_STATUS_ALL_APPLY.equals(payApplyState)
                || BusinessConstants.BUY_PAY_STATUS_DEPOSIT_APPLY.equals(payApplyState)
                || BusinessConstants.BUY_PAY_STATUS_DEPOSIT_PAID.equals(payApplyState)
                || BusinessConstants.BUY_PAY_STATUS_PART.equals(payApplyState)
                || BusinessConstants.BUY_PAY_STATUS_PAID.equals(payApplyState)
        ) {
            return AjaxResult.error("该采购单已经申请付款，不能退回！");
        }

        if (BusinessConstants.ARRIVE_STATUS_STORE_PASS.equals(lvDaohuo.getStatus())  || BusinessConstants.ARRIVE_STATUS_STORE_REJECT.equals(lvDaohuo.getStatus())) {
            return AjaxResult.error("该入库单已经审核！");
        }
        processArrivalService.insertProcess(lvDaohuo.getId(), "", "库管弃审");
        lvDaohuo.setStatus(BusinessConstants.ARRIVE_STATUS_STORE_REJECT);
        lvDaohuo.setShouhuoId(SecurityUtils.getUserId());
        lvDaohuo.setShourenName(SecurityUtils.getUsername());
        lvDaohuoService.update(lvDaohuo);
        LvDaohuoDetail lvDaohuoDetail = new LvDaohuoDetail();
        lvDaohuoDetail.setDaohuoId(id);
        List<LvDaohuoDetail> daohuoDetailList = lvDaohuoDetailService.selectList(lvDaohuoDetail);
        for (LvDaohuoDetail daohuoDetail : daohuoDetailList) {
            daohuoDetail.setShouhuoId(SecurityUtils.getUserId());
            daohuoDetail.setShourenName(SecurityUtils.getUsername());
            daohuoDetail.setInspectState(BusinessConstants.ARRIVE_STATUS_STORE_REJECT);
            lvDaohuoDetailService.update(daohuoDetail);
        }

        List<GoodsSaledo> goodsSaledoList = goodsSaledoMapper.selectByDaohuoId(lvDaohuo.getId());
        for (GoodsSaledo goodsSaledo : goodsSaledoList) {
            goodsSaledo.setStatus(BusinessConstants.SALE_TYPE_10);
            goodsSaledoMapper.update(goodsSaledo);
            if (goodsSaledo != null && goodsSaledo.getGoodsSaleId() != null) {
                goodsSaleService.updateStatusById(goodsSaledo.getGoodsSaleId(), BusinessConstants.SALE_TYPE_10);
            }
        }

        return AjaxResult.success("审核通过");
    }

    /**
     * 库管入库退货
     */
    @GetMapping(value = "/storeGoodsReturn/{id}")
    @Transactional
    public AjaxResult storeGoodsReturn(@PathVariable("id") String id) {
        LvDaohuo lvDaohuo = lvDaohuoService.selectById(id);
        // 对应采购单如果已经付款 则不能退回
        LvBuy lvBuy = lvBuyService.selectById(lvDaohuo.getBuyId());
        String payApplyState = lvBuy.getPayApplyState();
        if (BusinessConstants.BUY_PAY_STATUS_ALL_APPLY.equals(payApplyState)
                || BusinessConstants.BUY_PAY_STATUS_DEPOSIT_APPLY.equals(payApplyState)
                || BusinessConstants.BUY_PAY_STATUS_DEPOSIT_PAID.equals(payApplyState)
                || BusinessConstants.BUY_PAY_STATUS_PART.equals(payApplyState)
                || BusinessConstants.BUY_PAY_STATUS_PAID.equals(payApplyState)
        ) {
            return AjaxResult.error("该采购单已经申请付款，不能退回！");
        }

        if (!BusinessConstants.ARRIVE_STATUS_STORE_PASS.equals(lvDaohuo.getStatus())) {
            return AjaxResult.error("该入库单不是已入库状态，请检查！");
        }

        processArrivalService.insertProcess(lvDaohuo.getId(), "", "库管入库后退回");
        lvDaohuo.setStatus(BusinessConstants.ARRIVE_STATUS_STORE_REJECT);
        lvDaohuo.setShouhuoId(SecurityUtils.getUserId());
        lvDaohuo.setShourenName(SecurityUtils.getUsername());
        lvDaohuoService.update(lvDaohuo);

        LvDaohuoDetail lvDaohuoDetail = new LvDaohuoDetail();
        lvDaohuoDetail.setDaohuoId(id);
        List<LvDaohuoDetail> daohuoDetailList = lvDaohuoDetailService.selectList(lvDaohuoDetail);
        for (LvDaohuoDetail daohuoDetail : daohuoDetailList) {
            daohuoDetail.setShouhuoId(SecurityUtils.getUserId());
            daohuoDetail.setShourenName(SecurityUtils.getUsername());
            daohuoDetail.setInspectState(BusinessConstants.ARRIVE_STATUS_STORE_REJECT);
            lvDaohuoDetailService.update(daohuoDetail);
        }

        // 入库弃审  如果已入库 弃审之后减去库存 增加出库记录
        RukumingxiUtils.chukuByDaohuo(lvDaohuo);
        List<GoodsSaledo> goodsSaledoList = goodsSaledoMapper.selectByDaohuoId(lvDaohuo.getId());
        for (GoodsSaledo goodsSaledo : goodsSaledoList) {
            goodsSaledo.setStatus(BusinessConstants.SALE_TYPE_10);
            goodsSaledoMapper.update(goodsSaledo);
            if (goodsSaledo != null && goodsSaledo.getGoodsSaleId() != null) {
                goodsSaleService.updateStatusById(goodsSaledo.getGoodsSaleId(), BusinessConstants.SALE_TYPE_10);
            }
        }
        return AjaxResult.success("审核通过");
    }






    /**
     * 采购 退货出库
     */
    @GetMapping(value = "/buyGoodsReturn/{id}")
    @Transactional
    public AjaxResult buyGoodsReturn(@PathVariable("id") String id) {
        LvDaohuo lvDaohuo = lvDaohuoService.selectById(id);
        LvBuy lvBuy = lvBuyService.selectById(lvDaohuo.getBuyId());
        processArrivalService.insertProcess(lvDaohuo.getId(), "", "退货出库");
        String payApplyState = lvBuy.getPayApplyState();
        if (BusinessConstants.BUY_PAY_STATUS_ALL_APPLY.equals(payApplyState)
                || BusinessConstants.BUY_PAY_STATUS_PAID.equals(payApplyState)
        ) {
            return AjaxResult.error("该采购单已经申请付款，不能退回！");
        }
        lvBuy.setReturnStatus(BusinessConstants.RETURN_STATUS_OUT);
        lvBuyService.update(lvBuy);
        lvDaohuo.setReturnStatus(BusinessConstants.STATUS_YES);
        lvDaohuo.setShouhuoId(SecurityUtils.getUserId());
        lvDaohuo.setShourenName(SecurityUtils.getUsername());
        lvDaohuoService.update(lvDaohuo);

        LvDaohuoDetail lvDaohuoDetail = new LvDaohuoDetail();
        lvDaohuoDetail.setDaohuoId(id);
        List<LvDaohuoDetail> daohuoDetailList = lvDaohuoDetailService.selectList(lvDaohuoDetail);
        for (LvDaohuoDetail daohuoDetail : daohuoDetailList) {
            daohuoDetail.setShouhuoId(SecurityUtils.getUserId());
            daohuoDetail.setShourenName(SecurityUtils.getUsername());
            daohuoDetail.setInspectState(BusinessConstants.ARRIVE_STATUS_STORE_REJECT);
            lvDaohuoDetailService.update(daohuoDetail);
        }
        // 入库弃审  如果已入库 弃审之后减去库存 增加出库记录
        RukumingxiUtils.chukuByDaohuo(lvDaohuo);
        return AjaxResult.success("审核通过");
    }















}
